function [coords1,coords2] = plot_D_pattern
% This function is used to plot the pattern of D with various sort scheme
% first test my scheme, we follow the following steps
% generate for varouse degrees 1~4
% the coords of each row have only 3 nonzeros
d_max = 4;
coords1 = cell(d_max,1);
coords2 = cell(d_max,1);

%for the candidate scheme ,get the coordinates
for d = 1:d_max
    rows = d*(d+1)/2;
    coords = zeros(rows,3);
    % generate the local indice for convenience access
    [I1,J1,K1]=indices(d-1);
    coords(:,1) = q_me(I1+1,J1,K1);
    coords(:,2) = q_me(I1,J1+1,K1);
    coords(:,3) = q_me(I1,J1,K1+1);  
    coords1{d} = coords;
end
% and show it.
show_pattern(coords1);

% for other schemes
for d = 1:d_max
    rows = d*(d+1)/2;
    coords = zeros(rows,3);
    % generate the local indice for convenience access
    [I1,J1,K1]=indices_lai(d-1);
    coords(:,1) = q_lai(I1+1,J1,K1);
    coords(:,2) = q_lai(I1,J1+1,K1);
    coords(:,3) = q_lai(I1,J1,K1+1);  
    coords2{d} = coords;
end
show_pattern(coords2);

function q_me = q_me(i,j,k)
q_me = (j+k+1).*(j+k)/2 + k + 1;

function q_lai = q_lai(i,j,k)
d = i+j+k;
q_lai = (d+1).*(d+2)/2 - (i+j+1).*(i+j)/2 - i;

function [I,J,K] = indices_lai(d)
m = (d+1)*(d+2)/2;
I = zeros(m,1);
J = I;
K = I;
Mark = 1;
for j = d:(-1):0
   I(Mark:(Mark+j)) =[j:(-1):0]';
   J(Mark:(Mark+j)) =[0:j]';
   K(Mark:(Mark+j)) =(d-j)*ones(j+1,1);
   Mark = Mark+j+1;
end


function show_pattern(coords)
for d = 2:length(coords)
    coord = coords{d};
    rows = size(coord,1);
    cols = max(max(coord));
    pattern = zeros(rows,cols);
    for row = 1:rows
        pattern(row,coord(row,1)) = 1;
        pattern(row,coord(row,2)) = 2;
        pattern(row,coord(row,3)) = 3;
    end
    % I have get the pattern, then draw it on a picture;
    figure; 
    for row = 1:rows
        for col = 1: cols
            switch pattern(row,col)
                case 0
                    text(col*0.06 , 0.6 - row*0.06,'0','FontSize',12);
                case 1
                    text(col*0.06 , 0.6 - row*0.06,'\lambda_1','FontSize',12);
                case 2 
                    text(col*0.06 , 0.6 - row*0.06,'\lambda_2','FontSize',12);
                case 3
                    text(col*0.06 , 0.6 - row*0.06,'\lambda_3','FontSize',12);
                otherwise
                    fprintf('Error in drawing the pattern of D!');
            end
        end
    end
    axis tight; axis off; 
end